<!DOCTYPE HTML>
<html lang="zh-CN">


<head>
    <meta charset="utf-8">
    <meta name="keywords" content="Scrapy教程经典实战【新概念英语】, 阿狸的日志">
    <meta name="description" content="">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
    <meta name="renderer" content="webkit|ie-stand|ie-comp">
    <meta name="mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">
    <!-- Global site tag (gtag.js) - Google Analytics -->


    <title>Scrapy教程经典实战【新概念英语】 | 阿狸的日志</title>
    <link rel="icon" type="image/png" href="/favicon.png">

    <link rel="stylesheet" type="text/css" href="/libs/awesome/css/all.css">
    <link rel="stylesheet" type="text/css" href="/libs/materialize/materialize.min.css">
    <link rel="stylesheet" type="text/css" href="/libs/aos/aos.css">
    <link rel="stylesheet" type="text/css" href="/libs/animate/animate.min.css">
    <link rel="stylesheet" type="text/css" href="/libs/lightGallery/css/lightgallery.min.css">
    <link rel="stylesheet" type="text/css" href="/css/matery.css">
    <link rel="stylesheet" type="text/css" href="/css/my.css">

    <script src="/libs/jquery/jquery.min.js"></script>

<meta name="generator" content="Hexo 5.4.0"></head>




<body>
    <header class="navbar-fixed">
    <nav id="headNav" class="bg-color nav-transparent">
        <div id="navContainer" class="nav-wrapper container">
            <div class="brand-logo">
                <a href="/" class="waves-effect waves-light">
                    
                    <img src="/medias/logo.png" class="logo-img" alt="LOGO">
                    
                    <span class="logo-span">阿狸的日志</span>
                </a>
            </div>
            

<a href="#" data-target="mobile-nav" class="sidenav-trigger button-collapse"><i class="fas fa-bars"></i></a>
<ul class="right nav-menu">
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/" class="waves-effect waves-light">
      
      <i class="fas fa-home" style="zoom: 0.6;"></i>
      
      <span>首页</span>
    </a>
    
  </li>
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/tags" class="waves-effect waves-light">
      
      <i class="fas fa-tags" style="zoom: 0.6;"></i>
      
      <span>标签</span>
    </a>
    
  </li>
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/categories" class="waves-effect waves-light">
      
      <i class="fas fa-bookmark" style="zoom: 0.6;"></i>
      
      <span>分类</span>
    </a>
    
  </li>
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/archives" class="waves-effect waves-light">
      
      <i class="fas fa-archive" style="zoom: 0.6;"></i>
      
      <span>归档</span>
    </a>
    
  </li>
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/about" class="waves-effect waves-light">
      
      <i class="fas fa-user-circle" style="zoom: 0.6;"></i>
      
      <span>关于</span>
    </a>
    
  </li>
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/contact" class="waves-effect waves-light">
      
      <i class="fas fa-comments" style="zoom: 0.6;"></i>
      
      <span>留言板</span>
    </a>
    
  </li>
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/friends" class="waves-effect waves-light">
      
      <i class="fas fa-address-book" style="zoom: 0.6;"></i>
      
      <span>友情链接</span>
    </a>
    
  </li>
  
  <li>
    <a href="#searchModal" class="modal-trigger waves-effect waves-light">
      <i id="searchIcon" class="fas fa-search" title="搜索" style="zoom: 0.85;"></i>
    </a>
  </li>
</ul>


<div id="mobile-nav" class="side-nav sidenav">

    <div class="mobile-head bg-color">
        
        <img src="/medias/logo.png" class="logo-img circle responsive-img">
        
        <div class="logo-name">阿狸的日志</div>
        <div class="logo-desc">
            
            Never really desperate, only the lost of the soul.
            
        </div>
    </div>

    

    <ul class="menu-list mobile-menu-list">
        
        <li class="m-nav-item">
	  
		<a href="/" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-home"></i>
			
			首页
		</a>
          
        </li>
        
        <li class="m-nav-item">
	  
		<a href="/tags" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-tags"></i>
			
			标签
		</a>
          
        </li>
        
        <li class="m-nav-item">
	  
		<a href="/categories" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-bookmark"></i>
			
			分类
		</a>
          
        </li>
        
        <li class="m-nav-item">
	  
		<a href="/archives" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-archive"></i>
			
			归档
		</a>
          
        </li>
        
        <li class="m-nav-item">
	  
		<a href="/about" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-user-circle"></i>
			
			关于
		</a>
          
        </li>
        
        <li class="m-nav-item">
	  
		<a href="/contact" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-comments"></i>
			
			留言板
		</a>
          
        </li>
        
        <li class="m-nav-item">
	  
		<a href="/friends" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-address-book"></i>
			
			友情链接
		</a>
          
        </li>
        
        
    </ul>
</div>


        </div>

        
    </nav>

</header>

    



<div class="bg-cover pd-header post-cover" style="background-image: url('/medias/featureimages/21.jpg')">
    <div class="container" style="right: 0px;left: 0px;">
        <div class="row">
            <div class="col s12 m12 l12">
                <div class="brand">
                    <h1 class="description center-align post-title">Scrapy教程经典实战【新概念英语】</h1>
                </div>
            </div>
        </div>
    </div>
</div>




<main class="post-container content">

    
    <link rel="stylesheet" href="/libs/tocbot/tocbot.css">
<style>
    #articleContent h1::before,
    #articleContent h2::before,
    #articleContent h3::before,
    #articleContent h4::before,
    #articleContent h5::before,
    #articleContent h6::before {
        display: block;
        content: " ";
        height: 100px;
        margin-top: -100px;
        visibility: hidden;
    }

    #articleContent :focus {
        outline: none;
    }

    .toc-fixed {
        position: fixed;
        top: 64px;
    }

    .toc-widget {
        width: 345px;
        padding-left: 20px;
    }

    .toc-widget .toc-title {
        padding: 35px 0 15px 17px;
        font-size: 1.5rem;
        font-weight: bold;
        line-height: 1.5rem;
    }

    .toc-widget ol {
        padding: 0;
        list-style: none;
    }

    #toc-content {
        padding-bottom: 30px;
        overflow: auto;
    }

    #toc-content ol {
        padding-left: 10px;
    }

    #toc-content ol li {
        padding-left: 10px;
    }

    #toc-content .toc-link:hover {
        color: #42b983;
        font-weight: 700;
        text-decoration: underline;
    }

    #toc-content .toc-link::before {
        background-color: transparent;
        max-height: 25px;

        position: absolute;
        right: 23.5vw;
        display: block;
    }

    #toc-content .is-active-link {
        color: #42b983;
    }

    #floating-toc-btn {
        position: fixed;
        right: 15px;
        bottom: 76px;
        padding-top: 15px;
        margin-bottom: 0;
        z-index: 998;
    }

    #floating-toc-btn .btn-floating {
        width: 48px;
        height: 48px;
    }

    #floating-toc-btn .btn-floating i {
        line-height: 48px;
        font-size: 1.4rem;
    }
</style>
<div class="row">
    <div id="main-content" class="col s12 m12 l9">
        <!-- 文章内容详情 -->
<div id="artDetail">
    <div class="card">
        <div class="card-content article-info">
            <div class="row tag-cate">
                <div class="col s7">
                    
                          <div class="article-tag">
                            <span class="chip bg-color">无标签</span>
                          </div>
                    
                </div>
                <div class="col s5 right-align">
                    
                </div>
            </div>

            <div class="post-info">
                
                <div class="post-date info-break-policy">
                    <i class="far fa-calendar-minus fa-fw"></i>发布日期:&nbsp;&nbsp;
                    2022-07-03
                </div>
                

                

                

                

                
            </div>
        </div>
        <hr class="clearfix">

        
        <!-- 是否加载使用自带的 prismjs. -->
        <link rel="stylesheet" href="/libs/prism/prism.css">
        

        

        <div class="card-content article-card-content">
            <div id="articleContent">
                <h1 id="前言"><a href="#前言" class="headerlink" title="前言"></a>前言</h1><p>本教程旨在教会每一个想学习爬虫的人，学习的同时也能锻炼你阅读文档的习惯。</p>
<p>在本教程中，我们假设您的系统上已经安装了 Scrapy。如果不是这种情况，请参阅本小节下面环境配置。</p>
<p>我们将抓取<a target="_blank" rel="noopener" href="http://www.yytingli.com/category/xgnyy/nce-1" title="http://www.yytingli.com/category/xgnyy/nce-1">http://www.yytingli.com/category/xgnyy/nce-1</a>，这是一个新概念英语自学的网站。</p>
<p>本教程将引导您完成以下任务：</p>
<ol>
<li><p> 创建一个新的 Scrapy 项目</p>
</li>
<li><p> 编写<a target="_blank" rel="noopener" href="https://doc.scrapy.org/en/latest/topics/spiders.html#topics-spiders" title="爬虫">爬虫</a>爬取网站并提取数据</p>
</li>
<li><p> 使用命令行导出抓取的数据</p>
</li>
<li><p> 将蜘蛛更改为递归跟踪链接</p>
</li>
<li><p> 使用蜘蛛参数</p>
</li>
</ol>
<p>请严格按照要求进行开发，以避免开发中的错误。</p>
<p>开发平台：windows</p>
<p>IDE：PyCharm 2021.3.2 (Professional Edition)</p>
<p>语言版本：Python==3.9</p>
<p>框架版本：Scrapy==2.6.1</p>
<h1 id="一、网络爬虫介绍"><a href="#一、网络爬虫介绍" class="headerlink" title="一、网络爬虫介绍"></a>一、网络爬虫介绍</h1><blockquote>
<p>📌随着互联网的快速发展，越来越多的信息被发布到互联网上。这些信息都被嵌入到各 &#x20;<br>式各样的网站结构及样式当中，虽然搜索引擎可以辅助人们寻找到这些信息，但也拥有其 &#x20;<br>局限性。通用的搜索引擎的目标是尽可能覆盖全网络，其无法针对特定的目的和需求进行 &#x20;<br>索引。面对如今结构越来越复杂，且信息含量越来越密集的数据，通用的搜索引擎无法对 &#x20;<br>其进行有效的发现和获取。在这样的环境和需求的影响下，网络爬虫应运而生，它为互联 &#x20;<br>网数据的应用提供了新的方法。 &#x20;</p>
</blockquote>
<h1 id="二、Scrapy框架介绍"><a href="#二、Scrapy框架介绍" class="headerlink" title="二、Scrapy框架介绍"></a>二、Scrapy框架介绍</h1><blockquote>
<p>Scrapy is a fast high-level <a target="_blank" rel="noopener" href="https://en.wikipedia.org/wiki/Web_crawler" title="web crawling">web crawling</a> and <a target="_blank" rel="noopener" href="https://en.wikipedia.org/wiki/Web_scraping" title="web scraping">web scraping</a> framework, used to crawl websites and extract structured data from their pages. It can be used for a wide range of purposes, from data mining to monitoring and automated testing.</p>
</blockquote>
<p>根据官网的介绍:</p>
<p>Scrapy是一个快速的高级网络抓取和网络爬虫框架，用于抓取网站并从其页面中提取结构化数据。它可以用于广泛的目的，从数据挖掘到监测和自动测试。</p>
<p>Scrapy框架是基础Python开发，所以不会Python的同学看这里：</p>
<p><a target="_blank" rel="noopener" href="https://www.runoob.com/python3/python3-tutorial.html">https://www.runoob.com/python3/python3-tutorial.html</a></p>
<p><a target="_blank" rel="noopener" href="https://www.liaoxuefeng.com/wiki/1016959663602400">https://www.liaoxuefeng.com/wiki/1016959663602400</a></p>
<h1 id="三、准备开发环境"><a href="#三、准备开发环境" class="headerlink" title="三、准备开发环境"></a>三、准备开发环境</h1><h1 id="四、创建Scrapy项目"><a href="#四、创建Scrapy项目" class="headerlink" title="四、创建Scrapy项目"></a>四、创建Scrapy项目</h1><p>首先打开命令行窗口，输出命令：</p>
<pre class="line-numbers language-bash" data-language="bash"><code class="language-bash">scrapy startproject newconcept<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>

<p>在当前目录会有创建好的文件，文件夹的树状结构如下：</p>
<pre class="line-numbers language-bash" data-language="bash"><code class="language-bash">newconcept
    │  items.py
    │  middlewares.py
    │  pipelines.py
    │  settings.py
    │  __init__.py
    │
    ├─spiders
    │  │  
    │  │  __init__.py
    │  │
    │  └─__pycache__
    │          myspider.cpython-39.pyc
    │          __init__.cpython-39.pyc
    │
    └─__pycache__
            items.cpython-39.pyc
            settings.cpython-39.pyc
            __init__.cpython-39.pyc<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p>注意，<code>__pycache__</code>文件是pycharm自动生成的，不用管。</p>
<h1 id="五、创建第一个蜘蛛"><a href="#五、创建第一个蜘蛛" class="headerlink" title="五、创建第一个蜘蛛"></a>五、创建第一个蜘蛛</h1><pre class="line-numbers language-bash" data-language="bash"><code class="language-bash">scrapy genspider myspider www.yytingli.com<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>

<p>运行过后，发现在项目文件夹中的<code>spider</code>文件夹中会生成一个<code>myspider.py</code>文件。</p>
<p>大概是这样的：</p>
<pre class="line-numbers language-python" data-language="python"><code class="language-python"><span class="token keyword">import</span> scrapy

<span class="token keyword">class</span> <span class="token class-name">MyspiderSpider</span><span class="token punctuation">(</span>scrapy<span class="token punctuation">.</span>Spider<span class="token punctuation">)</span><span class="token punctuation">:</span>
    name <span class="token operator">=</span> <span class="token string">'myspider'</span>
    allowed_domains <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string">'www.yytingli.com'</span><span class="token punctuation">]</span>
    start_urls <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string">'http://www.yytingli.com'</span><span class="token punctuation">]</span>
    
    <span class="token keyword">def</span> <span class="token function">parse</span><span class="token punctuation">(</span>self<span class="token punctuation">,</span> response<span class="token punctuation">)</span><span class="token punctuation">:</span>
        <span class="token keyword">pass</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p>我们要做的就是将pass去掉然后编写我们的逻辑代码即可，<code>parse</code>函数接收一个<code>response</code>参数，这个参数就是网络请求的相应，我们只需要关心如何处理响应数据。</p>
<h1 id="六、网页分析"><a href="#六、网页分析" class="headerlink" title="六、网页分析"></a>六、网页分析</h1><p>网页分析分为两部分：</p>
<p>1.分析列表页，拿到详情页url。</p>
<p>2.分析详情页，爬取各部分数据。</p>
<p>我们使用Chrome浏览器打开新概念自学网站，<code>F12</code>打开开发者工具，并按<code>ctrl+shift+c</code> 检查网页元素，点击标题。</p>
<p><img src="image/image_3K7MQNoPyr.png"></p>
<p>如上图，可以看到右边网页元素<code>a</code>标签中的<code>href</code>属性就是详情页的链接地址。</p>
<p>网页元素定位的方法有很多，可以使用<code>bs4</code>库中的<code>find</code>方法，<code>select</code>方法。可以用<code>xpath</code>，可以用<code>pyquery </code>,直接用<code>javascript</code>操作<code>dom</code>元素，还可以直接使用标准库<code>re</code>，正则表达式来提取。</p>
<p>这里我们使用<code>xpath</code>语句定位和解析元素。</p>
<p>元素定位和解析网页具体方法不是本教程的重点，这里可以参考：</p>
<p><a target="_blank" rel="noopener" href="https://lxml.de/">https://lxml.de/</a></p>
<p>这里给大家布置一个小作业，根据以上方法对详情页网页结构进行分析，并编写代码，网址为：<a target="_blank" rel="noopener" href="http://www.yytingli.com/17.html" title="http://www.yytingli.com/17.html">http://www.yytingli.com/17.html</a>，我们提取的信息只有两个，文章的英文文本和翻译内容。</p>
<h1 id="七、列表页调试"><a href="#七、列表页调试" class="headerlink" title="七、列表页调试"></a>七、列表页调试</h1><p>首先需要修改<code>settings.py</code>文件中的配置参数</p>
<p>找到<code>DEFAULT_REQUEST_HEADERS </code>和<code>ROBOTSTXT_OBEY </code>参数，改成如下：</p>
<pre class="line-numbers language-bash" data-language="bash"><code class="language-bash">DEFAULT_REQUEST_HEADERS <span class="token operator">=</span> <span class="token punctuation">&#123;</span>
    <span class="token string">'Accept'</span><span class="token builtin class-name">:</span> <span class="token string">'text/html,application/xhtml+xml,application/xml;q=0.9, */*;q=0.8'</span>,
    <span class="token string">'Accept-Language'</span><span class="token builtin class-name">:</span> <span class="token string">'en'</span>,
    <span class="token string">'User-Agent'</span><span class="token builtin class-name">:</span> <span class="token string">'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) '</span>
                  <span class="token string">'Chrome/96.0.4664.93 Safari/537.36'</span>,
<span class="token punctuation">&#125;</span>
ROBOTSTXT_OBEY <span class="token operator">=</span> False
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p>Scrapy提供一个shell命令行的调试方法，我们可以使用如下命令进入调试：</p>
<pre class="line-numbers language-bash" data-language="bash"><code class="language-bash">scrapy shell http://www.yytingli.com/category/xgnyy/nce-1<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>

<p>经过分析网页后，我们使用已经编写好的测试代码：</p>
<pre class="line-numbers language-python" data-language="python"><code class="language-python"><span class="token keyword">for</span> i <span class="token keyword">in</span> response<span class="token punctuation">.</span>xpath<span class="token punctuation">(</span><span class="token string">'// *[@id="art_main"]/div/div[2]/h2/a'</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
    title <span class="token operator">=</span> i<span class="token punctuation">.</span>xpath<span class="token punctuation">(</span><span class="token string">'@title'</span><span class="token punctuation">)</span><span class="token punctuation">.</span>get<span class="token punctuation">(</span><span class="token punctuation">)</span>
    link <span class="token operator">=</span> i<span class="token punctuation">.</span>xpath<span class="token punctuation">(</span><span class="token string">'@href'</span><span class="token punctuation">)</span><span class="token punctuation">.</span>get<span class="token punctuation">(</span><span class="token punctuation">)</span>
    <span class="token keyword">print</span><span class="token punctuation">(</span>title<span class="token punctuation">,</span>link<span class="token punctuation">)</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span></span></code></pre>

<p>测试结果如下：</p>
<pre class="line-numbers language-python" data-language="python"><code class="language-python">In <span class="token punctuation">[</span><span class="token number">8</span><span class="token punctuation">]</span><span class="token punctuation">:</span>         <span class="token keyword">for</span> i <span class="token keyword">in</span> response<span class="token punctuation">.</span>xpath<span class="token punctuation">(</span><span class="token string">'// *[@id="art_main"]/div/div[2]/h2/a'</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
   <span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">:</span>             title <span class="token operator">=</span> i<span class="token punctuation">.</span>xpath<span class="token punctuation">(</span><span class="token string">'@title'</span><span class="token punctuation">)</span><span class="token punctuation">.</span>get<span class="token punctuation">(</span><span class="token punctuation">)</span>
   <span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">:</span>             link <span class="token operator">=</span> i<span class="token punctuation">.</span>xpath<span class="token punctuation">(</span><span class="token string">'@href'</span><span class="token punctuation">)</span><span class="token punctuation">.</span>get<span class="token punctuation">(</span><span class="token punctuation">)</span>
   <span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">:</span>             <span class="token keyword">print</span><span class="token punctuation">(</span>title<span class="token punctuation">,</span>link<span class="token punctuation">)</span>
   <span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">:</span>
新概念英语第一册<span class="token number">143</span>：A walk through the woods http<span class="token punctuation">:</span><span class="token operator">//</span>www<span class="token punctuation">.</span>yytingli<span class="token punctuation">.</span>com<span class="token operator">/</span><span class="token number">363.</span>html
新概念英语第一册<span class="token number">141</span>：Sally’s first train ride http<span class="token punctuation">:</span><span class="token operator">//</span>www<span class="token punctuation">.</span>yytingli<span class="token punctuation">.</span>com<span class="token operator">/</span><span class="token number">360.</span>html
新概念英语第一册<span class="token number">139</span>：Is that you<span class="token punctuation">,</span> John? http<span class="token punctuation">:</span><span class="token operator">//</span>www<span class="token punctuation">.</span>yytingli<span class="token punctuation">.</span>com<span class="token operator">/</span><span class="token number">357.</span>html
新概念英语第一册<span class="token number">137</span>：A pleasant dream http<span class="token punctuation">:</span><span class="token operator">//</span>www<span class="token punctuation">.</span>yytingli<span class="token punctuation">.</span>com<span class="token operator">/</span><span class="token number">354.</span>html
新概念英语第一册<span class="token number">135</span>：The latest report http<span class="token punctuation">:</span><span class="token operator">//</span>www<span class="token punctuation">.</span>yytingli<span class="token punctuation">.</span>com<span class="token operator">/</span><span class="token number">351.</span>html
新概念英语第一册<span class="token number">133</span>：Sensational news http<span class="token punctuation">:</span><span class="token operator">//</span>www<span class="token punctuation">.</span>yytingli<span class="token punctuation">.</span>com<span class="token operator">/</span><span class="token number">348.</span>html
新概念英语第一册<span class="token number">131</span>：Don’t be so sure http<span class="token punctuation">:</span><span class="token operator">//</span>www<span class="token punctuation">.</span>yytingli<span class="token punctuation">.</span>com<span class="token operator">/</span><span class="token number">345.</span>html
新概念英语第一册<span class="token number">129</span>：<span class="token number">70</span> miles an hour http<span class="token punctuation">:</span><span class="token operator">//</span>www<span class="token punctuation">.</span>yytingli<span class="token punctuation">.</span>com<span class="token operator">/</span><span class="token number">342.</span>html
新概念英语第一册<span class="token number">127</span>：A famous actress http<span class="token punctuation">:</span><span class="token operator">//</span>www<span class="token punctuation">.</span>yytingli<span class="token punctuation">.</span>com<span class="token operator">/</span><span class="token number">339.</span>html
新概念英语第一册<span class="token number">125</span>：Tea <span class="token keyword">for</span> two http<span class="token punctuation">:</span><span class="token operator">//</span>www<span class="token punctuation">.</span>yytingli<span class="token punctuation">.</span>com<span class="token operator">/</span><span class="token number">336.</span>html
新概念英语第一册<span class="token number">123</span>：A trip to Australia http<span class="token punctuation">:</span><span class="token operator">//</span>www<span class="token punctuation">.</span>yytingli<span class="token punctuation">.</span>com<span class="token operator">/</span><span class="token number">333.</span>html
新概念英语第一册<span class="token number">121</span>：The man <span class="token keyword">in</span> the hat http<span class="token punctuation">:</span><span class="token operator">//</span>www<span class="token punctuation">.</span>yytingli<span class="token punctuation">.</span>com<span class="token operator">/</span><span class="token number">330.</span>html
新概念英语第一册<span class="token number">119</span>：A true story http<span class="token punctuation">:</span><span class="token operator">//</span>www<span class="token punctuation">.</span>yytingli<span class="token punctuation">.</span>com<span class="token operator">/</span><span class="token number">327.</span>html
新概念英语第一册<span class="token number">117</span>：Tommy’s breakfast http<span class="token punctuation">:</span><span class="token operator">//</span>www<span class="token punctuation">.</span>yytingli<span class="token punctuation">.</span>com<span class="token operator">/</span><span class="token number">323.</span>html
新概念英语第一册<span class="token number">115</span>：Knock<span class="token punctuation">,</span> knock! http<span class="token punctuation">:</span><span class="token operator">//</span>www<span class="token punctuation">.</span>yytingli<span class="token punctuation">.</span>com<span class="token operator">/</span><span class="token number">320.</span>html
新概念英语第一册<span class="token number">113</span>：small change http<span class="token punctuation">:</span><span class="token operator">//</span>www<span class="token punctuation">.</span>yytingli<span class="token punctuation">.</span>com<span class="token operator">/</span><span class="token number">317.</span>html
新概念英语第一册<span class="token number">111</span>：The most expensive model http<span class="token punctuation">:</span><span class="token operator">//</span>www<span class="token punctuation">.</span>yytingli<span class="token punctuation">.</span>com<span class="token operator">/</span><span class="token number">314.</span>html
新概念英语第一册<span class="token number">109</span>：A good idea http<span class="token punctuation">:</span><span class="token operator">//</span>www<span class="token punctuation">.</span>yytingli<span class="token punctuation">.</span>com<span class="token operator">/</span><span class="token number">311.</span>html
新概念英语第一册<span class="token number">107</span>：It’s too small http<span class="token punctuation">:</span><span class="token operator">//</span>www<span class="token punctuation">.</span>yytingli<span class="token punctuation">.</span>com<span class="token operator">/</span><span class="token number">308.</span>html
新概念英语第一册<span class="token number">105</span>：Hello<span class="token punctuation">,</span> Mr<span class="token punctuation">.</span>boss http<span class="token punctuation">:</span><span class="token operator">//</span>www<span class="token punctuation">.</span>yytingli<span class="token punctuation">.</span>com<span class="token operator">/</span><span class="token number">305.</span>html
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p>以上，我们拿到了详情页url和标题，完成列表页的测试。</p>
<h1 id="八、详情页调试"><a href="#八、详情页调试" class="headerlink" title="八、详情页调试"></a>八、详情页调试</h1><p>我们可以使用如下命令进入调试：</p>
<pre class="line-numbers language-bash" data-language="bash"><code class="language-bash">scrapy shell http://www.yytingli.com/31.html<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>

<p>修改完毕后再次进入调试，我们使用已经编写好的测试代码：</p>
<pre class="line-numbers language-python" data-language="python"><code class="language-python">text <span class="token operator">=</span> response<span class="token punctuation">.</span>xpath<span class="token punctuation">(</span><span class="token string">'// *[@id="art_main1"]/div[2]/p[6]/text()'</span><span class="token punctuation">)</span><span class="token punctuation">.</span>getall<span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token comment"># 英文文本</span>
translation <span class="token operator">=</span> response<span class="token punctuation">.</span>xpath<span class="token punctuation">(</span><span class="token string">'// *[@id="art_main1"]/div[2]/p[8]/text()'</span><span class="token punctuation">)</span><span class="token punctuation">.</span>getall<span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token comment"># 文章翻译</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span></span></code></pre>

<p>我们输入text查看结果，如下：</p>
<pre class="line-numbers language-bash" data-language="bash"><code class="language-bash">In <span class="token punctuation">[</span><span class="token number">2</span><span class="token punctuation">]</span>: text
Out<span class="token punctuation">[</span><span class="token number">2</span><span class="token punctuation">]</span>:
<span class="token punctuation">[</span><span class="token string">'My coat and my umbrella please.'</span>,
 <span class="token string">'<span class="token entity" title="\n">\n</span>Here is my ticket.'</span>,
 <span class="token string">'<span class="token entity" title="\n">\n</span>Thank you, sir.'</span>,
 <span class="token string">'<span class="token entity" title="\n">\n</span>Number five.'</span>,
 <span class="token string">"<span class="token entity" title="\n">\n</span>Here's your umbrella and your coat."</span>,
 <span class="token string">'<span class="token entity" title="\n">\n</span>This is not my umbrella.'</span>,
 <span class="token string">'<span class="token entity" title="\n">\n</span>Sorry sir.'</span>,
 <span class="token string">'<span class="token entity" title="\n">\n</span>Is this your umbrella?'</span>,
 <span class="token string">"<span class="token entity" title="\n">\n</span>No, it isn't."</span>,
 <span class="token string">'<span class="token entity" title="\n">\n</span>Is this it?'</span>,
 <span class="token string">'<span class="token entity" title="\n">\n</span>Yes, it is.'</span>,
 <span class="token string">'<span class="token entity" title="\n">\n</span>Thank you very much.'</span><span class="token punctuation">]</span>

<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p>我们输入translation 查看结果，如下：</p>
<pre class="line-numbers language-python" data-language="python"><code class="language-python">In <span class="token punctuation">[</span><span class="token number">3</span><span class="token punctuation">]</span><span class="token punctuation">:</span> translation
Out<span class="token punctuation">[</span><span class="token number">3</span><span class="token punctuation">]</span><span class="token punctuation">:</span> 
<span class="token punctuation">[</span><span class="token string">'请把我的大衣和伞拿给我。'</span><span class="token punctuation">,</span>
 <span class="token string">'\n这是我（寄存东西）的牌子。'</span><span class="token punctuation">,</span>
 <span class="token string">'\n谢谢，先生。'</span><span class="token punctuation">,</span>
 <span class="token string">'\n是5号。'</span><span class="token punctuation">,</span>
 <span class="token string">'\n这是您的伞和大衣'</span><span class="token punctuation">,</span>
 <span class="token string">'\n这不是我的伞。'</span><span class="token punctuation">,</span>
 <span class="token string">'\n对不起，先生。'</span><span class="token punctuation">,</span>
 <span class="token string">'\n这把伞是您的吗？'</span><span class="token punctuation">,</span>
 <span class="token string">'\n不，不是！'</span><span class="token punctuation">,</span>
 <span class="token string">'\n这把是吗？'</span><span class="token punctuation">,</span>
 <span class="token string">'\n是，是这把。'</span><span class="token punctuation">,</span>
 <span class="token string">'\n非常感谢。'</span><span class="token punctuation">]</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p>以上完成了详情页的调试内容。</p>
<h1 id="九、编辑条目文件"><a href="#九、编辑条目文件" class="headerlink" title="九、编辑条目文件"></a>九、编辑条目文件</h1><p>经过调试，我们已经成功拿到数据，此时应该编写<code>items.py</code>文件，确定我们要爬取的内容。</p>
<pre class="line-numbers language-python" data-language="python"><code class="language-python"><span class="token comment"># Define here the models for your scraped items</span>
<span class="token comment">#</span>
<span class="token comment"># See documentation in:</span>
<span class="token comment"># https://docs.scrapy.org/en/latest/topics/items.html</span>

<span class="token keyword">import</span> scrapy


<span class="token keyword">class</span> <span class="token class-name">NewconceptItem</span><span class="token punctuation">(</span>scrapy<span class="token punctuation">.</span>Item<span class="token punctuation">)</span><span class="token punctuation">:</span>
    <span class="token comment"># 列表页</span>
    title <span class="token operator">=</span> scrapy<span class="token punctuation">.</span>Field<span class="token punctuation">(</span><span class="token punctuation">)</span>
    link <span class="token operator">=</span> scrapy<span class="token punctuation">.</span>Field<span class="token punctuation">(</span><span class="token punctuation">)</span>

    <span class="token comment"># 详情页</span>
    text <span class="token operator">=</span> scrapy<span class="token punctuation">.</span>Field<span class="token punctuation">(</span><span class="token punctuation">)</span>
    translation <span class="token operator">=</span> scrapy<span class="token punctuation">.</span>Field<span class="token punctuation">(</span><span class="token punctuation">)</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<h1 id="十、完成爬虫代码"><a href="#十、完成爬虫代码" class="headerlink" title="十、完成爬虫代码"></a>十、完成爬虫代码</h1><p>现在让我们完成<code>myspider.py</code>文件。</p>
<pre class="line-numbers language-python" data-language="python"><code class="language-python"><span class="token keyword">import</span> scrapy

<span class="token keyword">from</span> newconcept<span class="token punctuation">.</span>items <span class="token keyword">import</span> NewconceptItem


<span class="token keyword">class</span> <span class="token class-name">MyspiderSpider</span><span class="token punctuation">(</span>scrapy<span class="token punctuation">.</span>Spider<span class="token punctuation">)</span><span class="token punctuation">:</span>
    name <span class="token operator">=</span> <span class="token string">'myspider'</span>
    allowed_domains <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string">'www.yytingli.com'</span><span class="token punctuation">]</span>
    start_urls <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string">'http://www.yytingli.com/category/xgnyy/nce-1'</span><span class="token punctuation">]</span>

    <span class="token keyword">def</span> <span class="token function">parse</span><span class="token punctuation">(</span>self<span class="token punctuation">,</span> response<span class="token punctuation">)</span><span class="token punctuation">:</span>
        <span class="token keyword">for</span> i <span class="token keyword">in</span> response<span class="token punctuation">.</span>xpath<span class="token punctuation">(</span><span class="token string">'// *[@id="art_main"]/div/div[2]/h2/a'</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
            item <span class="token operator">=</span> NewconceptItem<span class="token punctuation">(</span><span class="token punctuation">)</span>
            title <span class="token operator">=</span> i<span class="token punctuation">.</span>xpath<span class="token punctuation">(</span><span class="token string">'@title'</span><span class="token punctuation">)</span><span class="token punctuation">.</span>get<span class="token punctuation">(</span><span class="token punctuation">)</span>
            link <span class="token operator">=</span> i<span class="token punctuation">.</span>xpath<span class="token punctuation">(</span><span class="token string">'@href'</span><span class="token punctuation">)</span><span class="token punctuation">.</span>get<span class="token punctuation">(</span><span class="token punctuation">)</span>
            item<span class="token punctuation">[</span><span class="token string">'link'</span><span class="token punctuation">]</span> <span class="token operator">=</span> link
            item<span class="token punctuation">[</span><span class="token string">'title'</span><span class="token punctuation">]</span> <span class="token operator">=</span> title
            <span class="token keyword">yield</span> scrapy<span class="token punctuation">.</span>Request<span class="token punctuation">(</span>url<span class="token operator">=</span>link<span class="token punctuation">,</span> meta<span class="token operator">=</span><span class="token punctuation">&#123;</span><span class="token string">'item'</span><span class="token punctuation">:</span> item<span class="token punctuation">&#125;</span><span class="token punctuation">,</span> callback<span class="token operator">=</span>self<span class="token punctuation">.</span>detail<span class="token punctuation">)</span>

    <span class="token keyword">def</span> <span class="token function">detail</span><span class="token punctuation">(</span>self<span class="token punctuation">,</span> response<span class="token punctuation">)</span><span class="token punctuation">:</span>
        item <span class="token operator">=</span> response<span class="token punctuation">.</span>meta<span class="token punctuation">[</span><span class="token string">'item'</span><span class="token punctuation">]</span>
        text <span class="token operator">=</span> response<span class="token punctuation">.</span>xpath<span class="token punctuation">(</span><span class="token string">'// *[@id="art_main1"]/div[2]/p[6]/text()'</span><span class="token punctuation">)</span><span class="token punctuation">.</span>getall<span class="token punctuation">(</span><span class="token punctuation">)</span>
        translation <span class="token operator">=</span> response<span class="token punctuation">.</span>xpath<span class="token punctuation">(</span><span class="token string">'// *[@id="art_main1"]/div[2]/p[8]/text()'</span><span class="token punctuation">)</span><span class="token punctuation">.</span>getall<span class="token punctuation">(</span><span class="token punctuation">)</span>
        item<span class="token punctuation">[</span><span class="token string">'text'</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token string">""</span><span class="token punctuation">.</span>join<span class="token punctuation">(</span>text<span class="token punctuation">)</span><span class="token punctuation">.</span>replace<span class="token punctuation">(</span><span class="token string">' '</span><span class="token punctuation">,</span> <span class="token string">''</span><span class="token punctuation">)</span><span class="token punctuation">.</span>replace<span class="token punctuation">(</span><span class="token string">'：'</span><span class="token punctuation">,</span> <span class="token string">''</span><span class="token punctuation">)</span>
        item<span class="token punctuation">[</span><span class="token string">'translation'</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token string">""</span><span class="token punctuation">.</span>join<span class="token punctuation">(</span>translation<span class="token punctuation">)</span><span class="token punctuation">.</span>replace<span class="token punctuation">(</span><span class="token string">' '</span><span class="token punctuation">,</span> <span class="token string">''</span><span class="token punctuation">)</span><span class="token punctuation">.</span>replace<span class="token punctuation">(</span><span class="token string">'：'</span><span class="token punctuation">,</span> <span class="token string">''</span><span class="token punctuation">)</span>
        <span class="token keyword">yield</span> item
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<p>切换到项目路径使用命令运行：</p>
<pre class="line-numbers language-bash" data-language="bash"><code class="language-bash">scrapy crawl myspider -o mydata.json
<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>

<p>程序完毕后会生成一个<code>mydata.json</code>文件，里面就是我们想要的内容。</p>
<p><code>start_urls </code>类属性是一个<code>list</code>类型，它决定你刚开始爬取的<code>url</code>列表，这里我们只用列表页的第一页进行爬取演示，如果你想爬取全部页内容，请将<code>start_urls </code>参数改成如下：</p>
<pre class="line-numbers language-python" data-language="python"><code class="language-python">start_urls <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string">'http://www.yytingli.com/category/xgnyy/nce-1'</span><span class="token punctuation">]</span><span class="token operator">+</span><span class="token punctuation">[</span><span class="token string">'http://www.yytingli.com/category/xgnyy/nce-1/page/%s'</span><span class="token operator">%</span>i <span class="token keyword">for</span> i <span class="token keyword">in</span> <span class="token builtin">range</span><span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">)</span><span class="token punctuation">]</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>

<p>查看文件部分内容如下：</p>
<pre class="line-numbers language-json" data-language="json"><code class="language-json">...
<span class="token punctuation">&#123;</span><span class="token property">"link"</span><span class="token operator">:</span> <span class="token string">"http://www.yytingli.com/348.html"</span><span class="token punctuation">,</span> <span class="token property">"title"</span><span class="token operator">:</span> <span class="token string">"\u65b0\u6982\u5ff5\u82f1\u8bed\u7b2c\u4e00\u518c133\uff1aSensational news"</span><span class="token punctuation">,</span> <span class="token property">"text"</span><span class="token operator">:</span> <span class="token string">"mink coat \u8c82\u76ae\u5927\u8863"</span><span class="token punctuation">,</span> <span class="token property">"translation"</span><span class="token operator">:</span> <span class="token string">"Reporter: Have you just made a new film, Miss Marsh?\nMiss Marsh: Yes, I have.\nReporter: Are you going to make another?\nMiss Marsh: No, I'm not. I'm going to retire. I feel very tired. I don't want to make another film for a long time."</span><span class="token punctuation">&#125;</span><span class="token punctuation">,</span>
<span class="token punctuation">&#123;</span><span class="token property">"link"</span><span class="token operator">:</span> <span class="token string">"http://www.yytingli.com/354.html"</span><span class="token punctuation">,</span> <span class="token property">"title"</span><span class="token operator">:</span> <span class="token string">"\u65b0\u6982\u5ff5\u82f1\u8bed\u7b2c\u4e00\u518c137\uff1aA pleasant dream"</span><span class="token punctuation">,</span> <span class="token property">"text"</span><span class="token operator">:</span> <span class="token string">"JULIE: Are you doing the football pools, Brain?\nBRAIN: Yes, I've nearly finished, Julie. I'm sure we'll win something this week.\nJULIE: You always say that, but we never win anything! What will you do if you win a lot of money?\nBRAIN: If I win a lot of money I'll buy you a mink coat.\nJULIE: I don't want a mink coat! I want to see the world.\nBRAIN: All right. If we win a lot of money we'll travel round the world and we'll stay at the best hotels. Then we'll return home and buy a big house in the country. We'll have a beautiful garden and...\nJULIE: But if we spend all that money we'll be poor again. What'll we do then?\nBRAIN: If we spend all the money we'll try and win the football pools again.\nJULIE: It's a pleasant dream but everything depends on 'if'!"</span><span class="token punctuation">,</span> <span class="token property">"translation"</span><span class="token operator">:</span> <span class="token string">"\u6731 \u8389\u5e03\u83b1\u6069\uff0c\u4f60\u6b63\u5728\u4e0b\u8db3\u7403\u8d5b\u7684\u8d4c\u6ce8\u5417\uff1f\n\u5e03\u83b1\u6069\u662f\u7684\u3002\u6211\u8fd9\u5c31\u505a\u5b8c\u4e86\uff0c\u6731\u8389\u3002\u6211\u6562\u80af\u5b9a\u8fd9\u661f\u671f\u6211\u4eec\u4f1a\u8d62\u4e00\u70b9\u4ec0\u4e48\u7684\u3002\n\u6731 \u8389\u4f60\u8001\u662f\u90a3\u6837\u8bf4\uff0c\u4f46\u662f\u6211\u4eec\u4ece\u6765\u6ca1\u8d62\u8fc7\uff01\u8981\u662f\u4f60\u8d62\u4e86\u8bb8\u591a\u94b1\uff0c\u4f60\u6253\u7b97\u505a\u4ec0\u4e48\u5462\uff1f\n\u5e03\u83b1\u6069\u8981\u662f\u6211\u8d62\u4e86\u8bb8\u591a\u94b1\uff0c\u6211\u7ed9\u4f60\u4e70\u4ef6\u8c82\u76ae\u5927\u8863\u3002\n\u6731 \u8389\u6211\u4e0d\u8981\u8c82\u76ae\u5927\u8863\u3002\u6211\u8981\u53bb\u89c1\u89c1\u4e16\u9762\u3002\n\u5e03\u83b1\u6069\u597d\u5427\u3002\u8981\u662f\u6211\u4eec\u8d62\u4e86\u5f88\u591a\u94b1\uff0c\u6211\u4eec\u5c31\u53bb\u5468\u6e38\u4e16\u754c\uff0c\u5e76\u4e14\u4f4f\u6700\u597d\u7684\u65c5\u9986\u3002\u7136\u540e\u6211\u4eec\u8fd4\u56de\u5bb6\u56ed\uff0c\u5728\u4e61\u4e0b\u4e70\u5e62\u5927\u623f\u5b50\u3002\u6211\u4eec\u5c06\u6709\u4e00\u4e2a\u6f02\u4eae\u7684\u82b1\u56ed\u548c......"</span><span class="token punctuation">&#125;</span><span class="token punctuation">,</span>
<span class="token punctuation">&#123;</span><span class="token property">"link"</span><span class="token operator">:</span> <span class="token string">"http://www.yytingli.com/357.html"</span><span class="token punctuation">,</span> <span class="token property">"title"</span><span class="token operator">:</span> <span class="token string">"\u65b0\u6982\u5ff5\u82f1\u8bed\u7b2c\u4e00\u518c139\uff1aIs that you, John?"</span><span class="token punctuation">,</span> <span class="token property">"text"</span><span class="token operator">:</span> <span class="token string">"GRAHAM TURNER: Is that you, John?\nJOHN SMITH: Yes, speaking.\nGRAHAM TURNER: Tell Mary we'll be late for dinner this evening.\nJOHN SMITH: I'm afraid I don't understand.\nGRAHAM TURNER: Hasn't Mary told you? She invited Charlotte and me to dinner this evening. I said I would be at your house at six o'clock, but the boss wants me to do some extra work. I'll have to stay at the office. I don't know when I'll finish. Oh, and by the way, my wife wants to know if Mary needs any help.\nJOHN SMITH: I don't know what you're talking about.\nGRAHAM TURNER: That is John Smith, isn't it?\nJOHN SMITH: Yes, I'm John Smith.\nGRAHAM TURNER: You are John Smith, the engineer, aren't you?\nJOHN SMITH: That's right.\nGRAHAM TURNER: You work for the Overseas Engineering Company, don't you?\nJOHN SMITH: No, I don't. I'm John Smith the telephone engineer and I'm repairing your telephone line."</span><span class="token punctuation">,</span> <span class="token property">"translation"</span><span class="token operator">:</span> <span class="token string">"\u683c\u96f7\u5384\u59c6\u00b7\u7279\u7eb3\u662f\u4f60\u5417\uff0c\u7ea6\u7ff0\uff1f\n\u7ea6\u7ff0\u00b7\u53f2\u5bc6\u65af\u662f\u6211\uff0c\u8bf7\u8bb2\u3002\n\u683c\u96f7\u5384\u59c6\u00b7\u7279\u7eb3\u4f60\u544a\u8bc9\u739b\u4e3d\uff0c\u4eca\u665a\u5403\u996d\u6211\u4eec\u5c06\u665a\u5230\u4e00\u4f1a\u513f\u3002\n\u7ea6\u7ff0\u00b7\u53f2\u5bc6\u65af\u6050\u6015\u6211\u8fd8\u4e0d\u660e\u767d\u60a8\u7684\u610f\u601d\u3002\n\u683c\u96f7\u5384\u59c6\u00b7\u7279\u7eb3\u739b\u4e3d\u6ca1\u6709\u544a\u8bc9\u4f60\u5417\uff1f\u5979\u9080\u8bf7\u6211\u548c\u590f\u6d1b\u7279\u4eca\u665a\u53bb\u5403\u996d\u3002\u6211\u8bf4\u8fc7\u62116\u70b9\u5230\u4f60\u5bb6\uff0c\u4f46\u8001\u677f\u8981\u6211\u52a0\u73ed\u3002\u6211\u4e0d\u5f97\u4e0d\u7559\u5728\u529e\u516c\u5ba4\uff0c\u4e0d\u77e5\u9053\u4ec0\u4e48\u65f6\u5019\u624d\u80fd\u7ed3\u675f\u3002\u5594\uff0c\u987a\u4fbf\u95ee\u4e00\u53e5\uff0c\u6211\u59bb\u5b50\u60f3\u77e5\u9053\u739b\u4e3d\u662f\u5426\u9700\u8981\u5e2e\u5fd9\u3002\n\u7ea6\u7ff0\u00b7\u53f2\u5bc6\u65af\u6211\u4e0d\u77e5\u9053\u60a8\u5728\u8bf4\u4e9b\u4ec0\u4e48\u3002\n\u683c\u96f7\u5384\u59c6\u00b7\u7279\u7eb3\u4f60\u662f\u7ea6\u7ff0\u00b7\u53f2\u5bc6\u65af\uff0c\u5bf9\u5417\uff1f\n\u7ea6\u7ff0\u00b7\u53f2\u5bc6\u65af\u662f\u7684\uff0c\u6211\u662f\u7ea6\u7ff0\u00b7\u53f2\u5bc6\u65af\u3002\n\u683c\u96f7\u5384\u59c6\uff0c\u7279\u7eb3\u4f60\u662f\u5de5\u7a0b\u5e08\u7ea6\u7ff0\u00b7\u53f2\u5bc6\u65af\uff0c\u5bf9\u5417\uff1f\n\u7ea6\u7ff0\u00b7\u53f2\u5bc6\u65af\u5bf9\u3002\n\u683c\u96f7\u5384\u59c6\u00b7\u7279\u7eb3\u4f60\u5728\u6d77\u5916\u5de5\u7a0b\u516c\u53f8\u4e0a\u73ed\uff0c\u662f\u5417\uff1f\n\u7ea6\u7ff0\u00b7\u53f2\u5bc6\u65af\u4e0d\uff0c\u4e0d\u662f\u3002\u6211\u662f\u7535\u8bdd\u5de5\u7a0b\u5e08\u7ea6\u7ff0\u00b7\u53f2\u5bc6\u65af\uff0c\u6211\u6b63\u5728\u4fee\u7406\u60a8\u7684\u7535\u8bdd\u7ebf\u3002\n\n"</span><span class="token punctuation">&#125;</span><span class="token punctuation">,</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span></span></code></pre>

<p>无需担心title中的中文显示问题，使用专业工具读取，或者python读取即可正常显示。</p>
<p>你可以加入QQ群与大家一起学习交流，这里我会给你解答学习中的问题。</p>
<p>点击链接加入群聊【Python】：<a target="_blank" rel="noopener" href="https://jq.qq.com/?_wv=1027&k=4fve4VeJ" title="https://jq.qq.com/?_wv=1027&amp;k=4fve4VeJ">https://jq.qq.com/?_wv=1027&amp;k=4fve4VeJ</a></p>

                
            </div>
            <hr/>

            

    <div class="reprint" id="reprint-statement">
        
            <div class="reprint__author">
                <span class="reprint-meta" style="font-weight: bold;">
                    <i class="fas fa-user">
                        文章作者:
                    </i>
                </span>
                <span class="reprint-info">
                    <a href="/about" rel="external nofollow noreferrer">hongzhe</a>
                </span>
            </div>
            <div class="reprint__type">
                <span class="reprint-meta" style="font-weight: bold;">
                    <i class="fas fa-link">
                        文章链接:
                    </i>
                </span>
                <span class="reprint-info">
                    <a href="http://huang-hongzhe.gitee.io/2022/07/03/Scrapy%E6%95%99%E7%A8%8B%E7%BB%8F%E5%85%B8%E5%AE%9E%E6%88%98%E3%80%90%E6%96%B0%E6%A6%82%E5%BF%B5%E8%8B%B1%E8%AF%AD%E3%80%91/">http://huang-hongzhe.gitee.io/2022/07/03/Scrapy%E6%95%99%E7%A8%8B%E7%BB%8F%E5%85%B8%E5%AE%9E%E6%88%98%E3%80%90%E6%96%B0%E6%A6%82%E5%BF%B5%E8%8B%B1%E8%AF%AD%E3%80%91/</a>
                </span>
            </div>
            <div class="reprint__notice">
                <span class="reprint-meta" style="font-weight: bold;">
                    <i class="fas fa-copyright">
                        版权声明:
                    </i>
                </span>
                <span class="reprint-info">
                    本博客所有文章除特別声明外，均采用
                    <a href="https://creativecommons.org/licenses/by/4.0/deed.zh" rel="external nofollow noreferrer" target="_blank">CC BY 4.0</a>
                    许可协议。转载请注明来源
                    <a href="/about" target="_blank">hongzhe</a>
                    !
                </span>
            </div>
        
    </div>

    <script async defer>
      document.addEventListener("copy", function (e) {
        let toastHTML = '<span>复制成功，请遵循本文的转载规则</span><button class="btn-flat toast-action" onclick="navToReprintStatement()" style="font-size: smaller">查看</a>';
        M.toast({html: toastHTML})
      });

      function navToReprintStatement() {
        $("html, body").animate({scrollTop: $("#reprint-statement").offset().top - 80}, 800);
      }
    </script>



            <div class="tag_share" style="display: block;">
                <div class="post-meta__tag-list" style="display: inline-block;">
                    
                        <div class="article-tag">
                            <span class="chip bg-color">无标签</span>
                        </div>
                    
                </div>
                <div class="post_share" style="zoom: 80%; width: fit-content; display: inline-block; float: right; margin: -0.15rem 0;">
                    <link rel="stylesheet" type="text/css" href="/libs/share/css/share.min.css">
<div id="article-share">

    

    

</div>

                </div>
            </div>
            
        </div>
    </div>

    

    

    

    

    

    

    

    

    

<article id="prenext-posts" class="prev-next articles">
    <div class="row article-row">
        
        <div class="article col s12 m6" data-aos="fade-up">
            <div class="article-badge left-badge text-color">
                <i class="fas fa-chevron-left"></i>&nbsp;上一篇</div>
            <div class="card">
                <a href="/2022/09/18/Vscode%E9%85%8D%E7%BD%AEC_C++%E7%8E%AF%E5%A2%83%E3%80%90%E9%80%82%E5%90%88%E5%B0%8F%E7%99%BD_%E5%85%8D%E7%8E%AF%E5%A2%83%E5%8F%98%E9%87%8F%E9%85%8D%E7%BD%AE%E3%80%91/">
                    <div class="card-image">
                        
                        
                        <img src="/medias/featureimages/8.jpg" class="responsive-img" alt="vsocde配置c/c++环境">
                        
                        <span class="card-title">vsocde配置c/c++环境</span>
                    </div>
                </a>
                <div class="card-content article-content">
                    <div class="summary block-with-text">
                        
                            
                        
                    </div>
                    <div class="publish-info">
                        <span class="publish-date">
                            <i class="far fa-clock fa-fw icon-date"></i>2022-09-18
                        </span>
                        <span class="publish-author">
                            
                            <i class="fas fa-bookmark fa-fw icon-category"></i>
                            
                            <a href="/categories/%E7%BC%96%E7%A8%8B/" class="post-category">
                                    编程
                                </a>
                            
                            
                        </span>
                    </div>
                </div>
                
                <div class="card-action article-tags">
                    
                    <a href="/tags/IDE/">
                        <span class="chip bg-color">IDE</span>
                    </a>
                    
                </div>
                
            </div>
        </div>
        
        
        <div class="article col s12 m6" data-aos="fade-up">
            <div class="article-badge right-badge text-color">
                下一篇&nbsp;<i class="fas fa-chevron-right"></i>
            </div>
            <div class="card">
                <a href="/2022/04/04/%E4%B8%93%E5%8D%87%E6%9C%AC%E6%8A%A5%E5%90%8D/">
                    <div class="card-image">
                        
                        
                        <img src="/medias/featureimages/23.jpg" class="responsive-img" alt="Selenium+pandas分析湖北专升本录取率">
                        
                        <span class="card-title">Selenium+pandas分析湖北专升本录取率</span>
                    </div>
                </a>
                <div class="card-content article-content">
                    <div class="summary block-with-text">
                        
                            
                        
                    </div>
                    <div class="publish-info">
                            <span class="publish-date">
                                <i class="far fa-clock fa-fw icon-date"></i>2022-04-04
                            </span>
                        <span class="publish-author">
                            
                            <i class="fas fa-user fa-fw"></i>
                            阿狸
                            
                        </span>
                    </div>
                </div>
                
            </div>
        </div>
        
    </div>
</article>

</div>



<!-- 代码块功能依赖 -->
<script type="text/javascript" src="/libs/codeBlock/codeBlockFunction.js"></script>

<!-- 代码语言 -->

<script type="text/javascript" src="/libs/codeBlock/codeLang.js"></script>


<!-- 代码块复制 -->

<script type="text/javascript" src="/libs/codeBlock/codeCopy.js"></script>


<!-- 代码块收缩 -->

<script type="text/javascript" src="/libs/codeBlock/codeShrink.js"></script>


<!-- 代码块折行 -->

<style type="text/css">
code[class*="language-"], pre[class*="language-"] { white-space: pre !important; }
</style>


    </div>
    <div id="toc-aside" class="expanded col l3 hide-on-med-and-down">
        <div class="toc-widget card" style="background-color: white;">
            <div class="toc-title"><i class="far fa-list-alt"></i>&nbsp;&nbsp;目录</div>
            <div id="toc-content"></div>
        </div>
    </div>
</div>

<!-- TOC 悬浮按钮. -->

<div id="floating-toc-btn" class="hide-on-med-and-down">
    <a class="btn-floating btn-large bg-color">
        <i class="fas fa-list-ul"></i>
    </a>
</div>


<script src="/libs/tocbot/tocbot.min.js"></script>
<script>
    $(function () {
        tocbot.init({
            tocSelector: '#toc-content',
            contentSelector: '#articleContent',
            headingsOffset: -($(window).height() * 0.4 - 45),
            collapseDepth: Number('0'),
            headingSelector: 'h2, h3, h4'
        });

        // modify the toc link href to support Chinese.
        let i = 0;
        let tocHeading = 'toc-heading-';
        $('#toc-content a').each(function () {
            $(this).attr('href', '#' + tocHeading + (++i));
        });

        // modify the heading title id to support Chinese.
        i = 0;
        $('#articleContent').children('h2, h3, h4').each(function () {
            $(this).attr('id', tocHeading + (++i));
        });

        // Set scroll toc fixed.
        let tocHeight = parseInt($(window).height() * 0.4 - 64);
        let $tocWidget = $('.toc-widget');
        $(window).scroll(function () {
            let scroll = $(window).scrollTop();
            /* add post toc fixed. */
            if (scroll > tocHeight) {
                $tocWidget.addClass('toc-fixed');
            } else {
                $tocWidget.removeClass('toc-fixed');
            }
        });

        
        /* 修复文章卡片 div 的宽度. */
        let fixPostCardWidth = function (srcId, targetId) {
            let srcDiv = $('#' + srcId);
            if (srcDiv.length === 0) {
                return;
            }

            let w = srcDiv.width();
            if (w >= 450) {
                w = w + 21;
            } else if (w >= 350 && w < 450) {
                w = w + 18;
            } else if (w >= 300 && w < 350) {
                w = w + 16;
            } else {
                w = w + 14;
            }
            $('#' + targetId).width(w);
        };

        // 切换TOC目录展开收缩的相关操作.
        const expandedClass = 'expanded';
        let $tocAside = $('#toc-aside');
        let $mainContent = $('#main-content');
        $('#floating-toc-btn .btn-floating').click(function () {
            if ($tocAside.hasClass(expandedClass)) {
                $tocAside.removeClass(expandedClass).hide();
                $mainContent.removeClass('l9');
            } else {
                $tocAside.addClass(expandedClass).show();
                $mainContent.addClass('l9');
            }
            fixPostCardWidth('artDetail', 'prenext-posts');
        });
        
    });
</script>

    

</main>




    <footer class="page-footer bg-color">
    
        <link rel="stylesheet" href="/libs/aplayer/APlayer.min.css">
<style>
    .aplayer .aplayer-lrc p {
        
        font-size: 12px;
        font-weight: 700;
        line-height: 16px !important;
    }

    .aplayer .aplayer-lrc p.aplayer-lrc-current {
        
        font-size: 15px;
        color: blue;
    }

    
</style>
<div class="">
    
    <div class="row">
        <meting-js class="col l8 offset-l2 m10 offset-m1 s12"
                   server="netease"
                   type="playlist"
                   id="6717903070"
                   fixed='true'
                   autoplay='true'
                   theme='blue'
                   loop='all'
                   order='random'
                   preload='auto'
                   volume='0.7'
                   list-folded='true'
        >
        </meting-js>
    </div>
</div>

<script src="/libs/aplayer/APlayer.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/meting@2/dist/Meting.min.js"></script>

    
    <div class="container row center-align" style="margin-bottom: 15px !important;">
        <div class="col s12 m8 l8 copy-right">
            Copyright&nbsp;&copy;
            
                <span id="year">2021-2022</span>
            
            <span id="year">2021</span>
            <a href="/about" target="_blank">hongzhe</a>
            |&nbsp;Powered by&nbsp;<a href="https://hexo.io/" target="_blank">Hexo</a>
            |&nbsp;Theme&nbsp;<a href="https://github.com/blinkfox/hexo-theme-matery" target="_blank">Matery</a>
            <br>
            
            
            
            
            
            
            <span id="busuanzi_container_site_pv">
                |&nbsp;<i class="far fa-eye"></i>&nbsp;总访问量:&nbsp;<span id="busuanzi_value_site_pv"
                    class="white-color"></span>&nbsp;次
            </span>
            
            
            <span id="busuanzi_container_site_uv">
                |&nbsp;<i class="fas fa-users"></i>&nbsp;总访问人数:&nbsp;<span id="busuanzi_value_site_uv"
                    class="white-color"></span>&nbsp;人
            </span>
            
            <br>
            
            <span id="sitetime">载入运行时间...</span>
            <script>
                function siteTime() {
                    var seconds = 1000;
                    var minutes = seconds * 60;
                    var hours = minutes * 60;
                    var days = hours * 24;
                    var years = days * 365;
                    var today = new Date();
                    var startYear = "2021";
                    var startMonth = "4";
                    var startDate = "11";
                    var startHour = "0";
                    var startMinute = "0";
                    var startSecond = "0";
                    var todayYear = today.getFullYear();
                    var todayMonth = today.getMonth() + 1;
                    var todayDate = today.getDate();
                    var todayHour = today.getHours();
                    var todayMinute = today.getMinutes();
                    var todaySecond = today.getSeconds();
                    var t1 = Date.UTC(startYear, startMonth, startDate, startHour, startMinute, startSecond);
                    var t2 = Date.UTC(todayYear, todayMonth, todayDate, todayHour, todayMinute, todaySecond);
                    var diff = t2 - t1;
                    var diffYears = Math.floor(diff / years);
                    var diffDays = Math.floor((diff / days) - diffYears * 365);
                    var diffHours = Math.floor((diff - (diffYears * 365 + diffDays) * days) / hours);
                    var diffMinutes = Math.floor((diff - (diffYears * 365 + diffDays) * days - diffHours * hours) /
                        minutes);
                    var diffSeconds = Math.floor((diff - (diffYears * 365 + diffDays) * days - diffHours * hours -
                        diffMinutes * minutes) / seconds);
                    if (startYear == todayYear) {
                        document.getElementById("year").innerHTML = todayYear;
                        document.getElementById("sitetime").innerHTML = "本站已安全运行 " + diffDays + " 天 " + diffHours +
                            " 小时 " + diffMinutes + " 分钟 " + diffSeconds + " 秒";
                    } else {
                        document.getElementById("year").innerHTML = startYear + " - " + todayYear;
                        document.getElementById("sitetime").innerHTML = "本站已安全运行 " + diffYears + " 年 " + diffDays +
                            " 天 " + diffHours + " 小时 " + diffMinutes + " 分钟 " + diffSeconds + " 秒";
                    }
                }
                setInterval(siteTime, 1000);
            </script>
            
            <br>
            
        </div>
        <div class="col s12 m4 l4 social-link social-statis">
    <a href="https://github.com/2528104776" class="tooltipped" target="_blank" data-tooltip="访问我的GitHub" data-position="top" data-delay="50">
        <i class="fab fa-github"></i>
    </a>



    <a href="mailto:2528104776@qq.com" class="tooltipped" target="_blank" data-tooltip="邮件联系我" data-position="top" data-delay="50">
        <i class="fas fa-envelope-open"></i>
    </a>







    <a href="tencent://AddContact/?fromId=50&fromSubId=1&subcmd=all&uin=2528104776" class="tooltipped" target="_blank" data-tooltip="QQ联系我: 2528104776" data-position="top" data-delay="50">
        <i class="fab fa-qq"></i>
    </a>







</div>
    </div>
</footer>

<div class="progress-bar"></div>


    <!-- 搜索遮罩框 -->
<div id="searchModal" class="modal">
    <div class="modal-content">
        <div class="search-header">
            <span class="title"><i class="fas fa-search"></i>&nbsp;&nbsp;搜索</span>
            <input type="search" id="searchInput" name="s" placeholder="请输入搜索的关键字"
                   class="search-input">
        </div>
        <div id="searchResult"></div>
    </div>
</div>

<script type="text/javascript">
$(function () {
    var searchFunc = function (path, search_id, content_id) {
        'use strict';
        $.ajax({
            url: path,
            dataType: "xml",
            success: function (xmlResponse) {
                // get the contents from search data
                var datas = $("entry", xmlResponse).map(function () {
                    return {
                        title: $("title", this).text(),
                        content: $("content", this).text(),
                        url: $("url", this).text()
                    };
                }).get();
                var $input = document.getElementById(search_id);
                var $resultContent = document.getElementById(content_id);
                $input.addEventListener('input', function () {
                    var str = '<ul class=\"search-result-list\">';
                    var keywords = this.value.trim().toLowerCase().split(/[\s\-]+/);
                    $resultContent.innerHTML = "";
                    if (this.value.trim().length <= 0) {
                        return;
                    }
                    // perform local searching
                    datas.forEach(function (data) {
                        var isMatch = true;
                        var data_title = data.title.trim().toLowerCase();
                        var data_content = data.content.trim().replace(/<[^>]+>/g, "").toLowerCase();
                        var data_url = data.url;
                        data_url = data_url.indexOf('/') === 0 ? data.url : '/' + data_url;
                        var index_title = -1;
                        var index_content = -1;
                        var first_occur = -1;
                        // only match artiles with not empty titles and contents
                        if (data_title !== '' && data_content !== '') {
                            keywords.forEach(function (keyword, i) {
                                index_title = data_title.indexOf(keyword);
                                index_content = data_content.indexOf(keyword);
                                if (index_title < 0 && index_content < 0) {
                                    isMatch = false;
                                } else {
                                    if (index_content < 0) {
                                        index_content = 0;
                                    }
                                    if (i === 0) {
                                        first_occur = index_content;
                                    }
                                }
                            });
                        }
                        // show search results
                        if (isMatch) {
                            str += "<li><a href='" + data_url + "' class='search-result-title'>" + data_title + "</a>";
                            var content = data.content.trim().replace(/<[^>]+>/g, "");
                            if (first_occur >= 0) {
                                // cut out 100 characters
                                var start = first_occur - 20;
                                var end = first_occur + 80;
                                if (start < 0) {
                                    start = 0;
                                }
                                if (start === 0) {
                                    end = 100;
                                }
                                if (end > content.length) {
                                    end = content.length;
                                }
                                var match_content = content.substr(start, end);
                                // highlight all keywords
                                keywords.forEach(function (keyword) {
                                    var regS = new RegExp(keyword, "gi");
                                    match_content = match_content.replace(regS, "<em class=\"search-keyword\">" + keyword + "</em>");
                                });

                                str += "<p class=\"search-result\">" + match_content + "...</p>"
                            }
                            str += "</li>";
                        }
                    });
                    str += "</ul>";
                    $resultContent.innerHTML = str;
                });
            }
        });
    };

    searchFunc('/search.xml', 'searchInput', 'searchResult');
});
</script>

    <!-- 回到顶部按钮 -->
<div id="backTop" class="top-scroll">
    <a class="btn-floating btn-large waves-effect waves-light" href="#!">
        <i class="fas fa-arrow-up"></i>
    </a>
</div>


    <script src="/libs/materialize/materialize.min.js"></script>
    <script src="/libs/masonry/masonry.pkgd.min.js"></script>
    <script src="/libs/aos/aos.js"></script>
    <script src="/libs/scrollprogress/scrollProgress.min.js"></script>
    <script src="/libs/lightGallery/js/lightgallery-all.min.js"></script>
    <script src="/js/matery.js"></script>

    <!-- Baidu Analytics -->

    <!-- Baidu Push -->

<script>
    (function () {
        var bp = document.createElement('script');
        var curProtocol = window.location.protocol.split(':')[0];
        if (curProtocol === 'https') {
            bp.src = 'https://zz.bdstatic.com/linksubmit/push.js';
        } else {
            bp.src = 'http://push.zhanzhang.baidu.com/push.js';
        }
        var s = document.getElementsByTagName("script")[0];
        s.parentNode.insertBefore(bp, s);
    })();
</script>

    
    <script src="/libs/others/clicklove.js" async="async"></script>
    
    
    <script async src="/libs/others/busuanzi.pure.mini.js"></script>
    

    

    

    <!--腾讯兔小巢-->
    
    

    

    

    
    <script src="/libs/instantpage/instantpage.js" type="module"></script>
    

</body>

</html>
